﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.Engine;
using Microsoft.Xna.Framework;

namespace SharedLib.Engine
{
    public enum LayerType
    {
        Background,
        Collision,
        Upper
    }

    public class Layer
    {
        private LayerType _type;
        public LayerType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        private byte[,] _cells;
        public byte[,] Cells
        {
            get { return _cells; }
            set { _cells = value; }
        }

        private List<Tile> _tiles;
        public List<Tile> Tiles
        {
            get { return _tiles; }
            set { _tiles = value; }
        }

        public void cellsToTiles(Vector2 startPosition)
        {
            startPosition *= new Vector2(Constants.CHUNK_WIDTH, Constants.CHUNK_HEIGHT) * new Vector2(Constants.TILE_SIZE,Constants.TILE_SIZE);
            Tiles = new List<Tile>();
            switch (_type)
            {
                
                case LayerType.Collision:
                    for (int x = 0; x < _cells.GetLength(0); x++)
                    {
                        for (int y = 0; y < _cells.GetLength(1); y++)
                        {
                            if (_cells[x, y] == 1)
                            {
                                Tile tuile = new Tile();
                                tuile.Position = new Microsoft.Xna.Framework.Vector2(startPosition.X + x * Constants.TILE_SIZE,startPosition.Y +  y * Constants.TILE_SIZE);
                                tuile.Value = _cells[x, y];
                                Tiles.Add(tuile);
                            }
                        }
                    }
                    break;
                case LayerType.Upper:
                case LayerType.Background:
                    for (int x = 0; x < _cells.GetLength(0); x++)
                    {
                        for (int y = 0; y < _cells.GetLength(1); y++)
                        {
                            
                            Tile tuile = new Tile();
                            tuile.Position = new Microsoft.Xna.Framework.Vector2(startPosition.X+ x * Constants.TILE_SIZE,startPosition.Y+ y * Constants.TILE_SIZE);
                            tuile.Value = _cells[x, y];
                            Tiles.Add(tuile);
                        }
                    }
                    break;
                default:
                    break;
            }
            
        }

        public Layer(int width,int height, LayerType type)
        {
            _type = type;
            _cells = new byte[width, height];
        }
        
    }
}
